package com.elitect.ftptransfer.module.login.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.elitect.ftptransfer.FtpTransferActivity;
import com.elitect.ftptransfer.R;
import com.elitect.ftptransfer.module.about.ui.AboutActivity;
import com.elitect.ftptransfer.module.help.ui.HelpActivity;
import com.elitect.ftptransfer.module.main.ui.MainActivity;
import com.elitect.ftptransfer.network.NetworkUtils;

public class LoginActivity extends FtpTransferActivity
{
    protected static final int DIALOG_NETWORK_NOT_AVAILABLE = 0;

    private View mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final EditText editTextServer = (EditText) findViewById(R.id.server_name);
        final EditText editTextPort = (EditText) findViewById(R.id.server_port);
        mProgressBar = findViewById(R.id.progress_bar);

        findViewById(R.id.button_login).setOnClickListener(
                new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        String server = editTextServer.getText().toString().trim();
                        String portStr = editTextPort.getText().toString().trim();

                        if (TextUtils.isEmpty(server)
                                || server.split("[.]").length < 2)
                        {
                            toastShort(getString(R.string.empty_input_server));
                            return;
                        }

                        int port = -1;
                        if (!TextUtils.isEmpty(portStr))
                        {
                            try
                            {
                                port = Integer.parseInt(portStr);
                            } catch (NumberFormatException e)
                            {
                            }

                            if (port < 0 && port > 65535)
                            {
                                toastShort(getString(R.string.error_input_wrong_port));
                                return;
                            }
                        }

                        if (NetworkUtils.isNetworkAvailable(LoginActivity.this))
                        {
                            NetworkUtils.doFtpConnect(server, port);
                            mProgressBar.setVisibility(View.VISIBLE);
                        }
                        else
                        {
                            showDialog(DIALOG_NETWORK_NOT_AVAILABLE);
                        }
                    }
                });

        findViewById(R.id.about).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(AboutActivity.class);
            }
        });
        findViewById(R.id.help).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(HelpActivity.class);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_CANCELED)
        {
            toastLong(getString(R.string.log_connect_lost));
        }
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        if (id == DIALOG_NETWORK_NOT_AVAILABLE)
        {
            return new AlertDialog.Builder(this).setIcon(
                    android.R.drawable.ic_dialog_alert).setTitle(
                    R.string.network).setPositiveButton(R.string.ok, null).setMessage(
                    R.string.error_network_not_available).create();
        }

        return super.onCreateDialog(id);
    }

    @Override
    public void onFtpConnectFail()
    {
        super.onFtpConnectFail();

        toastLong(getString(R.string.log_connect_fail));
        hideProgressBar();
    }

    @Override
    public void onFtpConnectSuccess()
    {
        super.onFtpConnectSuccess();

        EditText editTextUsername = (EditText) findViewById(R.id.username);
        EditText editTextPassword = (EditText) findViewById(R.id.password);
        String username = editTextUsername.getText().toString().trim();
        String password = editTextPassword.getText().toString();

        if (TextUtils.isEmpty(username))
        {
            toastShort(getString(R.string.empty_input_username));
            return;
        }

        if (TextUtils.isEmpty(password))
        {
            toastShort(getString(R.string.empty_input_password));
            return;
        }

        NetworkUtils.doFtpLogin(username, password);
    }

    @Override
    public void onFtpLoginFail()
    {
        super.onFtpLoginFail();

        toastLong(getString(R.string.log_login_fail));
        hideProgressBar();
    }

    @Override
    public void onFtpLoginSuccess()
    {
        super.onFtpLoginSuccess();

        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        hideProgressBar();
    }

    private void hideProgressBar()
    {
        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                mProgressBar.setVisibility(View.GONE);
            }
        });
    }
}
